package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    /*
    *根据品牌id 查询品牌信息
    * */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){

        Brand brand = brandService.getById(id);
        return ResponseEntity.ok(new BrandDTO(brand));
    }

    /*
     *根据品牌id集合  查询品牌集合信息
     * */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandsByIds(@RequestParam("ids") List<Long> ids){

        List<Brand> brandList = brandService.listByIds(ids);

        return ResponseEntity.ok(BrandDTO.convertEntityList(brandList));
    }

    /*
     *分页查询品牌信息
     *
     * 返回信息
     * PageDTO（BrandDTO）
     * */
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>> queryPage(
            @RequestParam(value = "page",defaultValue = "1") Long page,
            @RequestParam(value = "rows",defaultValue = "5") Long rows,
            @RequestParam(value = "key",required = false) String key
    ){

        return  ResponseEntity.ok(brandService.queryPage(page,rows,key));
    }

    //    根据分类id查询品牌
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id")Long id){
        return ResponseEntity.ok(brandService.queryBrandByCategoryId(id));
    }
     /*
     新增商品品牌
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO){
        brandService.addBrand(brandDTO);
        //CREATED 状态码201 成功
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /*
    更新商品品牌
    */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        //CREATED 状态码201 成功
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
}
